package com.xplay.xpocker.room.mahjong.dto;

import com.xplay.xpocker.constant.ConstantDict;
import com.xplay.xpocker.util.ObjectUtils;
import lombok.Builder;
import lombok.Data;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Data
public class MahjongMessage {
    String type;
    Integer cardCode;
    // 事件执行人
    BigDecimal exeActionUid;
    // 消息来自哪个用户
    BigDecimal messageFromUserId;
    // 消息发送给哪个用户
    Integer sourceBySeq;
    Integer cardType;
    List<Integer> swapCard;
    String data;
    LinkedHashSet<String> actionSet;

    public void addAction(String action) {
        if (actionSet == null) {
            actionSet = new LinkedHashSet<>();
        }
        actionSet.add(action);
    }

    public String getActionStr() {
        if (actionSet == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (String item : actionSet) {
            sb.append(item);
            sb.append(ConstantDict.Separator.comma);
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    public static class Builder {
        private MahjongMessage message;

        Builder(MahjongMessage message) {
            this.message = message;
        }

        public static Builder builder() {
            return new Builder(new MahjongMessage());
        }

        public Builder type(String type) {
            message.setType(type);
            return this;
        }

        public Builder action(String action) {
            message.addAction(action);
            return this;
        }

        public Builder data(String data) {
            message.setData(data);
            return this;
        }


        public Builder sourceBySeq(Integer id) {
            message.setSourceBySeq(id);
            return this;
        }

        public Builder cardType(Integer type) {
            message.setCardType(type);
            return this;
        }

        public Builder swapCard(List<Integer> swapCard) {
            message.setSwapCard(swapCard);
            return this;
        }


        public Builder cardCode(Integer cardCode) {
            message.setCardCode(cardCode);
            return this;
        }

        public Builder exeActionUid(BigDecimal userId) {
            message.setExeActionUid(userId);
            return this;
        }

        public Builder messageFromUserId(BigDecimal userId) {
            message.setMessageFromUserId(userId);
            return this;
        }

        public MahjongMessage build() {
            if (message.getType() == null) {
                throw new RuntimeException("！！！！！！！！！！！！！！！！事件类型不能为空！！！！！！！！！！！！！！");
            }
            if (!message.getType().equals(MahjongActionEnum.REFRESH.getAction()) &&
                    !message.getType().equals(MahjongActionEnum.NEXT_GAMING.getAction()) &&
                    message.getExeActionUid() == null) {
                throw new RuntimeException(String.format("????执行事件%s,未知!!!", message.getType()));
            }
            return this.message;
        }

    }

}